package one.stand.controller.order;

import com.jmp.api.dto.ShopCartAddRequest;
import com.jmp.api.dto.ShopCartEditRequest;
import com.jmp.api.dto.ShopCartListResponse;
import com.jmp.api.dto.ShopCartRemoveRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.ShopCartFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.Company2Dto;
import one.stand.dto.ConvertUtils;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "购物车")
@RestController
@RequestMapping("shop/cart")
public class ShopCartController {
    @Autowired
    private ShopCartFeign shopCartFeign;

    @Autowired
    private CompanyBaseService companyBaseService;

    @ApiOperation(value = "查询我的购物车")
    @GetMapping("/list")
    @SecurityAuthCheck
    public ResultModel<ShopCartListResponse> list() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel<ShopCartListVo> resultModel = shopCartFeign.list(ShopCartListDto.builder().userId(optUser.getUserId()).build());
        AssertUtil.isTrue(resultModel);

        List<ShopCartListResponse.Supplier> supplierList = ConvertUtils.list(resultModel.getData().getSupplierList(), supplier -> {

            Company2Dto company2Dto = companyBaseService.get2(supplier.getCompanyId());

            List<ShopCartListResponse.Goods> goodsList = ConvertUtils.list(supplier.getGoodsList(), goods -> {
                return ShopCartListResponse.Goods.builder()
                        .shopCartId(goods.getShopCartId())
                        .photo(goods.getPhoto())
                        .goodsId(goods.getGoodsId())
                        .goodsName(goods.getGoodsName())
                        .specName(goods.getSpecName() + "/" + goods.getSpecAttribute())
                        .specId(goods.getSpecId())
                        .num(goods.getNum())
                        .price(goods.getPrice())
                        .specNum(goods.getSpecNum())
                        .build();
            });

            return ShopCartListResponse.Supplier.builder()
                    .companyId(supplier.getCompanyId())
                    .companyLogo(company2Dto.getCompanyLogo())
                    .companyName(company2Dto.getCompanyName())
                    .goodsList(goodsList)
                    .build();
        });

        ShopCartListResponse response = ShopCartListResponse.builder()
                .supplierList(supplierList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "保存商品到购物车")
    @PostMapping("/add")
    @SecurityAuthCheck
    public ResultModel addShopcart(@RequestAttribute ShopCartAddRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel resultModel = shopCartFeign.add(ShopCartAddDto.builder()
                .userId(optUser.getUserId())
                .specId(request.getSpecId())
                .num(request.getNum())
                .cardId(null)
                .forwardId(null)
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "删除购物车商品")
    @PostMapping("/remove")
    @SecurityAuthCheck
    public ResultModel delShopcart(@RequestAttribute ShopCartRemoveRequest request) {
        AssertUtil.nonNull(request.getShopCartIds(), ResultEnum.PARAM_CHECK);

        ResultModel resultModel = shopCartFeign.remove(ShopCartRemoveDto.builder()
                .shopCartId(request.getShopCartIds())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @ApiOperation(value = "修改购物车商品数量")
    @PostMapping("/edit")
    @SecurityAuthCheck
    public ResultModel updShopcartNum(@RequestAttribute ShopCartEditRequest request) {
        ResultModel resultModel = shopCartFeign.edit(ShopCartEditDto.builder()
                .shopCartId(request.getShopCartId())
                .num(request.getNum())
                .build());
        AssertUtil.isTrue(resultModel);
        return ResultModel.success();
    }
}
